package thread;

public class DeadLock implements Runnable {
//    String name;
    static Object o1 = new Object();
    static Object o2 = new Object();
    int flag = 0;


    @Override
    public void run() {
        if (flag == 1) {
            System.out.println("flag=" + flag);
            synchronized (o1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2) {
                    System.out.println(1);
                }
            }
        }
        if (flag == 0) {
            System.out.println("flag=" + flag);
            synchronized (o2) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1) {
                    System.out.println(0);
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadLock deadLock1 = new DeadLock();
        DeadLock deadLock2 = new DeadLock();
        deadLock2.flag=1;
        Thread thread1 = new Thread(deadLock1);
        Thread thread2 = new Thread(deadLock2);
        thread1.start();
        thread2.start();
    }
}
/**
 * 模拟死锁发生的过程
 */
//class